<!DOCTYPE html>
<html lang="" xml:lang="">
<head>

  <meta charset="utf-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <title>4 统计 | 编程语言笔记</title>
  <meta name="description" content="4 统计 | 编程语言笔记" />
  <meta name="generator" content="bookdown 0.19 and GitBook 2.6.7" />

  <meta property="og:title" content="4 统计 | 编程语言笔记" />
  <meta property="og:type" content="book" />
  
  
  
  

  <meta name="twitter:card" content="summary" />
  <meta name="twitter:title" content="4 统计 | 编程语言笔记" />
  
  
  

<meta name="author" content="云腾足下" />



  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <meta name="apple-mobile-web-app-capable" content="yes" />
  <meta name="apple-mobile-web-app-status-bar-style" content="black" />
  
  
<link rel="prev" href="DataProcess.html"/>
<link rel="next" href="references.html"/>
<script src="libs/jquery-2.2.3/jquery.min.js"></script>
<link href="libs/gitbook-2.6.7/css/style.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-table.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-bookdown.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-highlight.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-search.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-fontsettings.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-clipboard.css" rel="stylesheet" />









<script type="text/x-mathjax-config">
MathJax.Hub.Config({
  jax: ["input/TeX","output/SVG"],
  extensions: ["tex2jax.js","MathMenu.js","MathZoom.js"],
  TeX: {
    extensions: ["AMSmath.js","AMSsymbols.js","noErrors.js","noUndefined.js"]
  }
});
</script>
<script type="text/javascript"
   src="../../../MathJax/MathJax.js">
</script>


<style type="text/css">
code.sourceCode > span { display: inline-block; line-height: 1.25; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode { white-space: pre; position: relative; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
code.sourceCode { white-space: pre-wrap; }
code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
  { counter-reset: source-line 0; }
pre.numberSource code > span
  { position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
  { content: counter(source-line);
    position: relative; left: -1em; text-align: right; vertical-align: baseline;
    border: none; display: inline-block;
    -webkit-touch-callout: none; -webkit-user-select: none;
    -khtml-user-select: none; -moz-user-select: none;
    -ms-user-select: none; user-select: none;
    padding: 0 4px; width: 4em;
    color: #aaaaaa;
  }
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa;  padding-left: 4px; }
div.sourceCode
  {   }
@media screen {
code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
</style>

</head>

<body>



  <div class="book without-animation with-summary font-size-2 font-family-1" data-basepath=".">

    <div class="book-summary">
      <nav role="navigation">

<ul class="summary">
<li class="chapter" data-level="" data-path="index.html"><a href="index.html"><i class="fa fa-check"></i>序</a></li>
<li class="chapter" data-level="1" data-path="Maple.html"><a href="Maple.html"><i class="fa fa-check"></i><b>1</b> Maple</a><ul>
<li class="chapter" data-level="1.1" data-path="Maple.html"><a href="Maple.html#一句话tips"><i class="fa fa-check"></i><b>1.1</b> 一句话Tips</a></li>
</ul></li>
<li class="chapter" data-level="2" data-path="Julia.html"><a href="Julia.html"><i class="fa fa-check"></i><b>2</b> Julia</a></li>
<li class="chapter" data-level="3" data-path="DataProcess.html"><a href="DataProcess.html"><i class="fa fa-check"></i><b>3</b> 数据处理</a><ul>
<li class="chapter" data-level="3.1" data-path="DataProcess.html"><a href="DataProcess.html#一句话tips-1"><i class="fa fa-check"></i><b>3.1</b> 一句话Tips</a></li>
<li class="chapter" data-level="3.2" data-path="DataProcess.html"><a href="DataProcess.html#rjsdmx包下载世界各大数据库数据"><i class="fa fa-check"></i><b>3.2</b> <code>RJSDMX</code>包下载世界各大数据库数据</a></li>
<li class="chapter" data-level="3.3" data-path="DataProcess.html"><a href="DataProcess.html#前向后向线性和样条插值"><i class="fa fa-check"></i><b>3.3</b> 前向、后向、线性和样条插值</a><ul>
<li class="chapter" data-level="3.3.1" data-path="DataProcess.html"><a href="DataProcess.html#signal包"><i class="fa fa-check"></i><b>3.3.1</b> <code>signal</code>包</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="4" data-path="统计.html"><a href="统计.html"><i class="fa fa-check"></i><b>4</b> 统计</a><ul>
<li class="chapter" data-level="4.1" data-path="统计.html"><a href="统计.html#一句话tips-2"><i class="fa fa-check"></i><b>4.1</b> 一句话Tips</a></li>
<li class="chapter" data-level="4.2" data-path="统计.html"><a href="统计.html#mcmc算法"><i class="fa fa-check"></i><b>4.2</b> MCMC算法</a><ul>
<li class="chapter" data-level="4.2.1" data-path="统计.html"><a href="统计.html#吉布斯抽样原理"><i class="fa fa-check"></i><b>4.2.1</b> 吉布斯抽样原理</a></li>
<li class="chapter" data-level="4.2.2" data-path="统计.html"><a href="统计.html#一些共轭先验分布的结论"><i class="fa fa-check"></i><b>4.2.2</b> 一些共轭先验分布的结论</a></li>
<li class="chapter" data-level="4.2.3" data-path="统计.html"><a href="统计.html#一个吉布斯抽样的典型案例"><i class="fa fa-check"></i><b>4.2.3</b> 一个吉布斯抽样的典型案例</a></li>
<li class="chapter" data-level="4.2.4" data-path="统计.html"><a href="统计.html#metropolis-和-m-h算法"><i class="fa fa-check"></i><b>4.2.4</b> Metropolis 和 M-H算法</a></li>
<li class="chapter" data-level="4.2.5" data-path="统计.html"><a href="统计.html#一些带贝叶斯估计的r包使用报告"><i class="fa fa-check"></i><b>4.2.5</b> 一些带贝叶斯估计的R包使用报告</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="" data-path="references.html"><a href="references.html"><i class="fa fa-check"></i>References</a></li>
</ul>

      </nav>
    </div>

    <div class="book-body">
      <div class="body-inner">
        <div class="book-header" role="navigation">
          <h1>
            <i class="fa fa-circle-o-notch fa-spin"></i><a href="./">编程语言笔记</a>
          </h1>
        </div>

        <div class="page-wrapper" tabindex="-1" role="main">
          <div class="page-inner">

            <section class="normal" id="section-">
<div id="统计" class="section level1">
<h1><span class="header-section-number">4</span> 统计</h1>
<div id="一句话tips-2" class="section level2">
<h2><span class="header-section-number">4.1</span> 一句话Tips</h2>
<ul>
<li><code>cmna::mcint</code>可以进行蒙特卡洛积分。</li>
<li>数值积分：<code>pracma::integral</code></li>
<li>多元正态分布随机抽样：<code>SimDesign::rmvnorm</code>，还有<code>mvnfast</code>。<code>mvtnorm</code>包是多变量正态分布的包，很全。</li>
<li><code>KSgeneral</code>包执行KS检验，比较一个分布是否来自某个理论分布。<code>stats</code>包的<code>ks.test</code>和<code>dgof</code>包的<code>ks.test</code>也可以,并且可以比较双样本是否来自同一个分布。</li>
<li><code>choose(n,k)</code>：组合公式，n个里面选k个，有多少种组合方式。<code>utils::combn(n,k)</code>也可以。 <code>e1071::permutations</code>实现排列。</li>
<li><code>qrandom</code>: 利用量子波动产生真随机数.</li>
<li>主成分分析可以调用<code>psych</code>包两个步骤实现：</li>
</ul>
<div class="sourceCode" id="cb13"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb13-1"><a href="统计.html#cb13-1"></a><span class="co"># 画个图选特征值数目：</span></span>
<span id="cb13-2"><a href="统计.html#cb13-2"></a><span class="co"># 1. 特征值在1以上的才行； 2. 特征值大于模拟的平均特征才可行； 3. 碎石图</span></span>
<span id="cb13-3"><a href="统计.html#cb13-3"></a><span class="kw">library</span>(psych)</span>
<span id="cb13-4"><a href="统计.html#cb13-4"></a><span class="kw">fa.parallel</span>(regdata, <span class="dt">fa =</span> <span class="st">&#39;pc&#39;</span>)</span>
<span id="cb13-5"><a href="统计.html#cb13-5"></a><span class="co"># 计算2个主成分。如果想要主成分载荷更有经济意义，注意设置旋转参数</span></span>
<span id="cb13-6"><a href="统计.html#cb13-6"></a> <span class="kw">principal</span>(regdata,<span class="dt">nfactors =</span> <span class="dv">2</span>,<span class="dt">rotate =</span> <span class="st">&#39;none&#39;</span>)</span></code></pre></div>
</div>
<div id="mcmc算法" class="section level2">
<h2><span class="header-section-number">4.2</span> MCMC算法</h2>
<div id="吉布斯抽样原理" class="section level3">
<h3><span class="header-section-number">4.2.1</span> 吉布斯抽样原理</h3>
<p>如果联合分布不好求，但条件分布好求，可以用这个算法。</p>
</div>
<div id="一些共轭先验分布的结论" class="section level3">
<h3><span class="header-section-number">4.2.2</span> 一些共轭先验分布的结论</h3>
<p>理解这些结论，对于后续使用吉布斯抽样、MH算法非常有用。</p>
<p><strong>结论1</strong> 若<span class="math inline">\(x_1,\cdots,x_n\)</span>是从均值为<span class="math inline">\(\mu\)</span>(<strong>未知</strong>)，方差为<span class="math inline">\(\sigma^2\)</span>(<strong>已知</strong>且为正)中正态分布中所抽取的一个随机样本，同时假定<span class="math inline">\(\mu\sim \mathcal{N}(\mu_0,\sigma_0^2)\)</span>，则给定数据和先验分布，<span class="math inline">\(\mu\)</span>的后验分布也是一个正态分布，其后验均值和方差为，
<span class="math display">\[\mu_* = \frac{\sigma^2\mu_0+n\sigma_0^2\overline x}{\sigma^2+n\sigma_0^2},\hspace{2em}\sigma_*=\frac{\sigma^2\sigma^2_0}{\sigma^2+n\sigma^2_0},\;\;\;\text{其中},\overline x= \sum_i^n x_i/n\]</span></p>
<p>推广到多变量，则可以写为，
<span class="math display">\[{\mu}_*=\Sigma_*(\Sigma_0^{-1}{\mu}_0+\Sigma^{-1}\overline{\bf{x}}), \hspace{2em}\Sigma_*^{-1} = \Sigma_0^{-1}+n\Sigma^{-1}\]</span></p>
<p><strong>结论2</strong> 若<span class="math inline">\(e_1,\cdots,e_n\)</span>是从均值为0，方差为<span class="math inline">\(\sigma^2\)</span>的正态分布中抽取的随机样本，同时假定<span class="math inline">\(\sigma^2\)</span>的先验分布是自由度为<span class="math inline">\(\nu\)</span>的逆<span class="math inline">\(\chi^2\)</span>分布，即<span class="math inline">\(\frac{\nu\lambda}{\sigma^2}\sim \chi^2_\nu,\lambda&gt;0\)</span>，则<span class="math inline">\(\sigma^2\)</span>的后验分布也是逆<span class="math inline">\(\chi^2\)</span>分布，自由度为<span class="math inline">\(\nu+n\)</span>，
<span class="math display">\[\frac{\nu\lambda+\sum_i^ne_i^2}{\sigma^2}\sim \chi^2_{\nu+n}\]</span></p>
</div>
<div id="一个吉布斯抽样的典型案例" class="section level3">
<h3><span class="header-section-number">4.2.3</span> 一个吉布斯抽样的典型案例</h3>
<p>一个带自相关的回归模型可以写为，
<span class="math display">\[\begin{align}
y_t&amp;=\beta_0+\beta_1x_{1t}+\cdots+\beta_kx_{kt}+z_t\\
z_t&amp;=\phi z_{t-1}+e_t
\end{align}\]</span></p>
<p>该模型需要估计的参数有三个，即<span class="math inline">\(\theta = (\beta&#39;,\phi,\sigma^2)\)</span>。该参数的联合分布并不好求，但是条件分布则好求得多。</p>
</div>
<div id="metropolis-和-m-h算法" class="section level3">
<h3><span class="header-section-number">4.2.4</span> Metropolis 和 M-H算法</h3>
<p>如果后验分布除了那个归一化的常数不知道，但分子是知道的，那可以用这个算法。这个场景是不是在贝叶斯估计中很熟悉？</p>
<p><code>MCMCpack::MCMCmetrop1R</code>中有个例子提供了Metropolis算法，感觉还是很清晰。里面提到的’The proposal distribution’其实就是跳跃分布，即给定上一次抽样的参数，从这个跳跃分布中抽下一个参数。</p>
</div>
<div id="一些带贝叶斯估计的r包使用报告" class="section level3">
<h3><span class="header-section-number">4.2.5</span> 一些带贝叶斯估计的R包使用报告</h3>
<ul>
<li><code>MTS::BVAR</code>：这个包可以在一个一般的先验设定上估计VAR，先验可以是乏信息先验，也可以是明尼苏达先验，但问题是该包仅返回估计系数的均值和标准误，不返回抽样。</li>
<li><code>bvartools</code>：在很大程度上可以定制BVAR的mcmc抽样，见它的一个优秀的引言。我用这个,自己写了乏信息先验的BVAR估计包。下次我再把明尼苏达先验添进去。</li>
<li><code>MCMCpack::MCMCregress</code>:单方程的贝叶斯估计，它提供了<span class="math inline">\(\beta\)</span>是多元正态先验，方程误差项的方差协方差是逆伽玛的先验估计。</li>
<li><code>bayesm::runireg</code>：单方程的贝叶斯估计，它提供了<span class="math inline">\(\beta\)</span>是多元正态先验，方程误差项的方差协方差是卡方分布的先验估计。</li>
</ul>

</div>
</div>
</div>
            </section>

          </div>
        </div>
      </div>
<a href="DataProcess.html" class="navigation navigation-prev " aria-label="Previous page"><i class="fa fa-angle-left"></i></a>
<a href="references.html" class="navigation navigation-next " aria-label="Next page"><i class="fa fa-angle-right"></i></a>
    </div>
  </div>
<script src="libs/gitbook-2.6.7/js/app.min.js"></script>
<script src="libs/gitbook-2.6.7/js/lunr.js"></script>
<script src="libs/gitbook-2.6.7/js/clipboard.min.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-search.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-sharing.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-fontsettings.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-bookdown.js"></script>
<script src="libs/gitbook-2.6.7/js/jquery.highlight.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-clipboard.js"></script>
<script>
gitbook.require(["gitbook"], function(gitbook) {
gitbook.start({
"sharing": {
"github": false,
"facebook": true,
"twitter": true,
"linkedin": false,
"weibo": false,
"instapaper": false,
"vk": false,
"all": ["facebook", "twitter", "linkedin", "weibo", "instapaper"]
},
"fontsettings": {
"theme": "white",
"family": "sans",
"size": 2
},
"edit": {
"link": null,
"text": null
},
"history": {
"link": null,
"text": null
},
"view": {
"link": null,
"text": null
},
"download": ["CBook.pdf"],
"toc": {
"collapse": "subsection"
}
});
});
</script>

<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
  (function () {
    var script = document.createElement("script");
    script.type = "text/javascript";
    var src = "true";
    if (src === "" || src === "true") src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-MML-AM_CHTML";
    if (location.protocol !== "file:")
      if (/^https?:/.test(src))
        src = src.replace(/^https?:/, '');
    script.src = src;
    document.getElementsByTagName("head")[0].appendChild(script);
  })();
</script>
</body>

</html>
